/*
 * Copyright (C), 2014-2015, 杭州小卡科技有限公司
 * Created by ciwei@xiaokakeji.com on 2016/06/21.
 */
package com.micromall.repository;

import com.micromall.repository.entity.AdminUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ciwei@xiaokakeji.com
 * @date 2016/06/21.
 */
@Repository
public interface AdminUserMapper {

	@Select("SELECT `id`, `username`, `password`, `name`, `internal`, `enable`, `create_time` createTime, `update_time` updateTime FROM `admin_user`"
			+ " order by create_time desc")
	@ResultType(AdminUser.class)
	List<AdminUser> selectList();

	@Select("SELECT `id`, `username`, `password`, `name`, `internal`, `enable`, `create_time` createTime, `update_time` updateTime FROM `admin_user`"
			+ " where `id` = #{id}")
	@ResultType(AdminUser.class)
	AdminUser get(@Param("id") int id);

	@Insert("INSERT INTO `admin_user`(`id`,`username`,`password`,`name`,`internal`,`enable`,`create_time`,`update_time`) VALUES (#{id},#{username},"
			+ "#{password},#{name},0,#{enable},#{createTime},null);")
	@SelectKey(before = false, keyProperty = "id", resultType = Integer.class, statementType = StatementType.STATEMENT, statement = "SELECT "
			+ "LAST_INSERT_ID() AS id")
	int insert(AdminUser adminUser);

	@Delete("DELETE FROM `admin_user` WHERE id=#{id} and internal=0")
	int delete(@Param("id") int id);

	@Update("UPDATE `admin_user` SET `username` = #{username}, `password` = #{password}, `name` = #{name}, `enable` = #{enable}, `update_time` = "
			+ "#{updateTime} WHERE `id` = #{id}")
	int update(AdminUser adminUser);

	@Update("UPDATE `admin_user` SET `password` = #{password}, `enable` = #{enable}, `update_time` = #{updateTime} WHERE `id` = #{id}")
	int updatePassword(AdminUser adminUser);

	@Select("select count(*) FROM `admin_user` where `username` = #{username}")
	@ResultType(Integer.class)
	int existsUsername(@Param("username") String username);

	@Select("select count(*) FROM `admin_user` where `username` = #{username} and `id` != #{id}")
	@ResultType(Integer.class)
	int existsUsernameInUpdate(@Param("id") int id, @Param("username") String username);

	@Select("select password FROM `admin_user` where `username` = #{username}")
	@ResultType(String.class)
	String selectPasswordByUsername(@Param("username") String username);

	@Select("SELECT `id`, `username`, `name`, `internal` FROM `admin_user` where `username` = #{username}")
	@ResultType(AdminUser.class)
	AdminUser getByUsername(@Param("username") String username);
}
